<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class YidongController extends Controller
{
    public function index()
    {
    	$list = DB::table('yidong')->get();
    	return view('admin.yidong.index',['list'=>$list]);
    }
    public function destroy($id)
    {
    	DB::table('yidong')->where('id',$id)->delete();
    	return redirect('admin/yidong');
    }
    public function create()
    {
    	return view('admin.yidong.add');
    }
    public function store(Request $request)
    {
        $phone = null;
        $pad = null;
        $other = null;
    	if ($request->hasFile('phone')) {
            if ($request->file('phone')->isValid()) {
                $file = $request->file('phone');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $phone = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$phone);
            }else{
                return back()->with("msg","图片格式不正确,请重新添加");
            }
        }
        if ($request->hasFile('pad')){
            if ($request->file('pad')->isValid()) {
                $file = $request->file('pad');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $pad = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$pad);
            }else{
                return back()->with("msg","图片格式不正确,请重新添加");
            }
        }
        if ($request->hasFile('other')){
            if ($request->file('other')->isValid()) {
                $file = $request->file('other');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $other = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$other);
            }else{
                return back()->with("msg","图片格式不正确,请重新添加");
            }
         }
        if ($phone == null && $pad == null && $other == null) {
           return back()->with("msg","图片添加失败,请注意选择图片");
        }else{
            $da = array('phone'=>$phone,'pad'=>$pad,'other'=>$other);
            $data = array_filter($da);
            $m = DB::table('yidong')->insertGetId($data);
            if ($m>0) {
                return redirect('admin/yidong');
            }else{
                return back()->with("msg","图片添加失败,请重新添加");
            }
        } 
    }
    public function edit($id)
    {
    	$list = DB::table('yidong')->where('id',$id)->first();
    	return view('admin.yidong.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
        $phone = null;
        $pad = null;
        $other = null;
    	if ($request->hasFile('phone')) {
            if ($request->file('phone')->isValid()) {
                $file = $request->file('phone');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $phone = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$phone);
            }else{
                return back()->with("msg","图片格式不正确,请重新修改");
            }
        }
        if ($request->hasFile('pad')){
            if ($request->file('pad')->isValid()) {
                $file = $request->file('pad');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $pad = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$pad);
            }else{
                return back()->with("msg","图片格式不正确,请重新修改");
            }
        }
        if ($request->hasFile('other')){
            if ($request->file('other')->isValid()) {
                $file = $request->file('other');
                // dd($file);
                $ext = $file->getClientOriginalExtension();
                $other = time().rand(1000,9999).'.'.$ext;
                // 保存图片
                $file->move('./menu/',$other);
            }else{
                return back()->with("msg","图片格式不正确,请重新修改");
            }
         }
        if ($phone == null && $pad == null && $other == null) {
           return back()->with("msg","图片修改失败,请注意选择图片");
        }else{
            $da = array('phone'=>$phone,'pad'=>$pad,'other'=>$other);
            $data = array_filter($da);
            $m = DB::table('yidong')->where('id',$id)->update($data);
            if ($m>0) {
                return redirect('admin/yidong');
            }else{
                return redirect('admin/yidong');
            }
        } 
    }
}
